package com.zhubayi.daka.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhubayi.daka.mapper.MyUserMapper;
import com.zhubayi.daka.pojo.DateTable;
import com.zhubayi.daka.pojo.MyUser;
import com.zhubayi.daka.utils.DakaUtil;
import com.zhubayi.daka.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

/**
 * @author 祝八一
 */
@Controller
public class DakaController {
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    MyUserMapper myUserMapper;
    @Autowired
    DakaUtil dakaUtil;
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/autodaka")
    public String autodaka(){
        return "autodaka";
    }
    @PostMapping("/daka")
    public String daka(@RequestParam("username")String username, @RequestParam("password") String password, RedirectAttributes model, HttpServletResponse response){
        if (username!=null&&password!=null&&username!=""&&password!=""){
            ConcurrentMap<String, Object> map = dakaUtil.loginAndDaka(username, password);
            String msg = (String)map.get("msg");
            List<DateTable> table = (List<DateTable>)map.get("table");
            model.addFlashAttribute("msg",msg);
            model.addFlashAttribute("table",table);
            System.out.println("学号:"+username+"手动打卡消息:"+msg+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return "redirect:/index";
        }
        return "redirect:/index";
    }
    @PostMapping("/add")
    public String add(MyUser myUser,RedirectAttributes attributes){
        if (myUser.getEmail()!=null&&myUser.getUsername()!=null&&myUser.getPassword()!=null&&""!=myUser.getEmail()&&""!=myUser.getUsername()&&""!=myUser.getPassword()){
            MyUser user = myUserMapper.selectOne(new QueryWrapper<MyUser>().eq("username", myUser.getUsername()));
            if(null!=user){
                attributes.addFlashAttribute("msg",myUser.getUsername()+"已存在");
                return "redirect:/autodaka";
            }
            ConcurrentMap<String, Object> map = dakaUtil.loginAndDaka(myUser.getUsername(), myUser.getPassword());
            String msg = (String)map.get("msg");
            if("密码不对!".equals(msg)){
                attributes.addFlashAttribute("msg","密码不对!");
                return "redirect:/autodaka";
            }
            attributes.addFlashAttribute("msg","设置成功");
            myUserMapper.insert(myUser);
            return "redirect:/index";
        }
        attributes.addFlashAttribute("msg","输入出错");
        return "redirect:/autodaka";
    }
    @PostMapping("/cancel")
    public String cancel(@RequestParam("username") String username,RedirectAttributes attributes){
        int count = myUserMapper.delete(new QueryWrapper<MyUser>().eq("username", username));
        if(count>0){
            attributes.addFlashAttribute("msg","取消成功");
            return "redirect:/index";
        }
        attributes.addFlashAttribute("msg","取消失败");
        return "redirect:/index";
    }
    @PostMapping("/updateemail")
    public String update(MyUser myUser,RedirectAttributes attributes){
        if (myUser.getEmail()!=null&&myUser.getUsername()!=null&&myUser.getPassword()!=null&&""!=myUser.getEmail()&&""!=myUser.getUsername()&&""!=myUser.getPassword()){
            MyUser user = myUserMapper.selectOne(new QueryWrapper<MyUser>().eq("username", myUser.getUsername()));
            if(null==user){
                attributes.addFlashAttribute("msg","该用户不存在");
                return "redirect:/toupdate";
            }
            if(!user.getPassword().equals(myUser.getPassword())){
                attributes.addFlashAttribute("msg","密码不正确");
                return "redirect:/toupdate";
            }
            user.setEmail(myUser.getEmail());
            int count = myUserMapper.update(user, new UpdateWrapper<MyUser>().eq("username", user.getUsername()));
            if(count==1){
                mailUtil.sendUpdate(user);
                attributes.addFlashAttribute("msg","邮箱更新成功");
                return "redirect:/index";
            }
            attributes.addFlashAttribute("msg","邮箱更新失败");
            return "redirect:/toupdate";
        }
        attributes.addFlashAttribute("msg","输入有误");
        return "redirect:/toupdate";
    }
    @GetMapping("/tocancel")
    public String tocancel(){
        return "cancel";
    }

    @GetMapping("/toupdate")
    public String toupdate(){
        return "update";
    }

}
